package svc.elib.linkpred;

import java.util.Collection;
import java.util.Iterator;

import svc.elib.db.Author;
import svc.elib.socnet.Net;

public class Jaccard implements LPMetric {

	@Override
	public double score(Author a, Author b, Net n) {
		int numCommons = 0;
		Collection<Author> bNeis = n.getGraph().getNeighbors(b);
		Collection<Author> aNeis = n.getGraph().getNeighbors(a);
		
		Iterator<Author> anit = aNeis.iterator();
		while (anit.hasNext()) {
			Author anei = anit.next();
			if (bNeis.contains(anei)) 
				++numCommons;
		}
		
		int denom = bNeis.size() + aNeis.size() - numCommons;
		if (denom == 0)
			return 0;
		
		return (double) numCommons / (double) denom;
	}

}
